<template>
<div class="app-container calendar-list-container">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" :label-position="'left'" label-width="100px" class="demo-ruleForm">
      <el-row>
        <el-col :span="11">
          <div class="filter-container">
            <router-link :to="path">
              <span class="back-link"><svg-icon icon-class="ds-left-double-arrow" /> 返回</span>
            </router-link>
          </div>
        </el-col>
      </el-row>
      <h3>编辑Tips</h3>
      <el-row class="left-block">
        <el-col :span="18">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>基础信息</span>
            </div>
            <el-form-item label="类型" prop="region">
              <el-select v-model="ruleForm.region" placeholder="请选择类型">
                <el-option v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item class="merge merge-width" label="孕周" prop="name">
              孕<el-input class="short-input" v-model="ruleForm.name" maxlength="2"></el-input>周，
            </el-form-item>
            <el-form-item class="merge1" prop="name1">
              第<el-input class="short-input" v-model="ruleForm.name1" maxlength="1"></el-input>天
            </el-form-item>
            <div class="num-wrong">孕周仅支持0-40，天数仅支持0-6</div>
          </el-card>
        </el-col>
      </el-row>
      <el-row class="right-block">
        <el-col :span="18" :offset="1">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>Tips信息</span>
            </div>
            <el-form-item class="merge" label="身高/体重" prop="name2">
              <el-input v-model="ruleForm.name2" placeholder="请输入身高"></el-input>
            </el-form-item>
            <el-form-item class="merge2" prop="name3">
              <el-input v-model="ruleForm.name3" placeholder="请输入体重"></el-input>
            </el-form-item>
            <el-form-item label="活动形式" prop="desc">
              <el-input type="textarea" v-model="ruleForm.desc" maxlength="80"></el-input>
            </el-form-item>
          </el-card>
        </el-col>
      </el-row>
      <div>
          <el-button class="button" type="primary" @click="submitForm('ruleForm')">保存</el-button>
      </div>
    </el-form>

  </div>
</template>
<style scoped>
  .el-form-item__content {
    color: #666;
  }
  .el-col-18 {
    width: 90%;
  }
  .el-col-18  >>> .el-card__body {
    height: 300px;
  }
  .text {
    font-size: 14px;
  }
  .item {
    margin-bottom: 28px;
  }
  .button {
    float: right;
    margin: 50px 100px;
  }
  .clearfix:before,
  .clearfix:after {
    display: table;
    content: "";
  }
  .left-block, .right-block {
    width: 50%;
    float: left;
  }
  .classify {
    margin-bottom: 28px;
  }
  .classify > span {
    color: #109ef0;
    font-size: 15px;
    margin: 0 15px;
    display: inline-block;
  }
  .el-select {
    width: 50%;
  }
  .short-input {
    width: 60px;
    display: inline-block;
    margin: 0 10px;
  }
  .num-wrong {
    color: #999;
    margin: -20px 0 0 100px;
    font-size: 14px;
  }
  .el-form-item {
    margin-bottom: 50px;
  }
  .merge, .merge1, .merge2 {
    display: inline-block;
  }
  .merge, .merge2 {
    width: 50%;
  }
  .merge-width {
    width: auto!important;
  }
  .merge1{
    margin-left: -90px;
  }
  .merge2{
    margin-left: -60px;
  }
  .el-textarea {
    width: 75%;
  }
  .el-textarea >>> .el-textarea__inner {
    height: 120px !important;
  }
</style>
<script>
  import { getUserInfo, saveUserInfo } from '@/api/tips'
  export default {
    data() {
      const checkWeek = (rule, value, callback) => {
        setTimeout(() => {
          if (!Number.isInteger(parseInt(value))) {
            callback(new Error('请输入数字值'))
          } else {
            if (parseInt(value) > 40 || parseInt(value) < 0) {
              callback(new Error('孕周仅支持0-40'))
            } else {
              callback()
            }
          }
        }, 1000)
      }
      const checkDay = (rule, value, callback) => {
        setTimeout(() => {
          if (!Number.isInteger(parseInt(value))) {
            callback(new Error('请输入数字值'))
          } else {
            if (parseInt(value) > 6 || parseInt(value) < 0) {
              callback(new Error('天数仅支持0-6'))
            } else {
              callback()
            }
          }
        }, 1000)
      }
      return {
        path: '/partner/index',
        info: {},
        options: [{
          value: '0',
          label: '孕期'
        }],
        ruleForm: {
          name: '',
          name1: '',
          name2: '',
          name3: '',
          region: '孕期',
          desc: ''
        },
        rules: {
          name: [
            { required: true, message: '请输入数字', trigger: 'blur' },
            { min: 0, max: 2, message: '长度在 1 到 2 个字符', trigger: 'blur' },
            { validator: checkWeek, trigger: 'blur' }
          ],
          name1: [
            { required: true, message: '请输入数字', trigger: 'blur' },
            { min: 0, max: 1, message: '长度为 1 个字符', trigger: 'blur' },
            { validator: checkDay, trigger: 'blur' }
          ],
          name2: [
            { required: true, message: '请输入数字', trigger: 'blur' },
            { min: 0, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' },
            { pattern: /^[0-9a-zA-Z\.]+$/, message: '不允许输入中文和特殊符号' }
          ],
          name3: [
            { required: true, message: '请输入数字', trigger: 'blur' },
            { min: 0, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' },
            { pattern: /^[0-9a-zA-Z\.]+$/, message: '不允许输入中文和特殊符号' }
          ],
          desc: [
            { required: true, message: '请填写活动形式', trigger: 'blur' },
            { min: 0, max: 80, message: '长度在 1 到 80 个字符', trigger: 'blur' }

          ],
          region: [
            { required: true, message: '请选择活动区域', trigger: 'change' }
          ]
        }
      }
    },
    created() {
      this.getUserInfo()
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (this.ruleForm.region === '孕期') {
              this.ruleForm.region = 0
            }
            const param = {
              tip_id: this.$route.params.id,
              pregnancy_week: this.ruleForm.name,
              pregnancy_day: this.ruleForm.name1,
              height: this.ruleForm.name2,
              weight: this.ruleForm.name3,
              title: this.ruleForm.title,
              type: this.ruleForm.region
            }
            saveUserInfo(param).then(response => {
              this.$router.push({ path: '/science/tip-index/' })
            })
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      getUserInfo() {
        getUserInfo({ tip_id: this.$route.params.id }).then(response => {
          this.info = response.data.tipInfo
          this.ruleForm = {
            name: this.info.pregnancy_week,
            name1: this.info.pregnancy_day,
            name2: this.info.height,
            name3: this.info.weight,
            region: this.info.type_name,
            desc: this.info.title
          }
        })
      }
    },
    watch: {
      name: function() {
        this.ruleForm.name = this.ruleForm.name.replace(/[\W]/g, '')
      }
    }
  }
</script>
